package com.hospital.phyexam.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.phyexam.dao.PhyexamBatchMapper;
import com.hospital.phyexam.dao.PhyexamProjectMapper;
import com.hospital.phyexam.dao.RelationPhyBatchMapper;
import com.hospital.phyexam.entity.*;
import com.hospital.phyexam.service.PhyexamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PhyexamServiceImpl implements PhyexamService {
    @Autowired
    PhyexamBatchMapper phyexamBatchMapper;
    @Autowired
    RelationPhyBatchMapper relationPhyBatchMapper;
    @Autowired
    PhyexamProjectMapper phyexamProjectMapper;

    public List getAll(PhyexamBatch phyexamBatch){

    List<Map<String,String>> resaultmap=new ArrayList<>();
    List<RelationPhyBatchKey> relationPhyBatchKeys=relationPhyBatchMapper.selectByBatchId(phyexamBatch.getId());
          for (RelationPhyBatchKey relationPhyBatchKey:relationPhyBatchKeys){
              Map<String,String>map=new HashMap<>();
              map.put("proId",phyexamProjectMapper.selectByPrimaryKey(relationPhyBatchKey.getRltPhyProId()).getId().toString());
              map.put("id",phyexamBatch.getId().toString());
            map.put("proName",phyexamProjectMapper.selectByPrimaryKey(relationPhyBatchKey.getRltPhyProId()).getProName());
            map.put("phyExmBatchName",phyexamBatch.getPhyExmBatchName());
            map.put("phyExmBatchBeginTime",new SimpleDateFormat("yyyy-MM-dd").format(phyexamBatch.getPhyExmBatchBeginTime()));
            map.put("phyExmBatchOverTime",new SimpleDateFormat("yyyy-MM-dd").format(phyexamBatch.getPhyExmBatchOverTime()));
       resaultmap.add(map);


    }

    return resaultmap;
}

    @Override
    public String getAllBatch(){
        List<Map<String,String>> resaultmap=new ArrayList<>();
        PhyexamBatchExample phyexamBatchExample =new PhyexamBatchExample();
        PhyexamBatchExample.Criteria criteria =phyexamBatchExample.createCriteria();
        criteria.getAllCriteria();
        List<PhyexamBatch> phyexamBatches=phyexamBatchMapper.selectByExample(phyexamBatchExample);
        for (PhyexamBatch phyexamBatch:phyexamBatches){
            resaultmap.addAll(getAll(phyexamBatch));
        }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public String selectByBatchName(String name){
        PhyexamBatch phyexamBatch=phyexamBatchMapper.selectByBatchName(name);
        return JSON.toJSONString(getAll(phyexamBatch));
    }

    @Override
    public String delete(int proid,int batchId){
        List<RelationPhyBatchKey>relationPhyBatchKeys=relationPhyBatchMapper.selectByBatchId(batchId);
        for (RelationPhyBatchKey relationPhyBatchKey:relationPhyBatchKeys){
            if (relationPhyBatchKey.getRltPhyProId()==proid)
            relationPhyBatchMapper.deleteByPrimaryKey(relationPhyBatchKey);
        }
        return getAllBatch();
    }

    @Override
    public String update(int batchId,int proId,String proName){
        PhyexamProject phyexamProject=phyexamProjectMapper.selectByProName(proName);
        List<RelationPhyBatchKey>relationPhyBatchKeys=relationPhyBatchMapper.selectByBatchId(batchId);
        for (RelationPhyBatchKey relationPhyBatchKey:relationPhyBatchKeys){
            if (relationPhyBatchKey.getRltPhyProId()==proId){
                RelationPhyBatchKey relationPhyBatchKey1=new RelationPhyBatchKey();
                relationPhyBatchKey1.setRltPhyProId(phyexamProject.getId());
                relationPhyBatchKey1.setRltPhyExamBatchId(relationPhyBatchKey.getRltPhyExamBatchId());
                relationPhyBatchMapper.deleteByPrimaryKey(relationPhyBatchKey);
                relationPhyBatchMapper.insert(relationPhyBatchKey1);

            }
        }
        return getAllBatch();
    }

}